var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: '',
    mobile: '',
    code: '',
    disableGetMobileCode: false,
    disableSubmitMobileCode: true,
    getCodeButtonText: '发送'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  ipt_password: function (e) {
    this.setData({
      password: e.detail.value
    });
  },
  ipt_mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  ipt_code: function (e) {
    this.setData({
      code: e.detail.value
    });
  },
  send_code: function () {
    var that = this;
    var mobile = that.data.mobile;
    if (mobile == '' || mobile == undefined) {
      wx.showModal({
        content: '请输入手机号码'
      });
      return;
    }
    util.request(api.SmsCode, {
      phone: mobile
    }, 'GET').then(function (res) {
      if (res.Status === 100) {
        wx.showModal({
          content: '验证码发送成功'
        });
        return;
      } else {
        wx.showModal({
          content: '验证码发送失败'
        });
        return;
      }
    });

  },
  countDownPassCode: function () {
    var that = this;
    var mobile = that.data.mobile;
    if (mobile == '' || mobile == undefined) {
      wx.showModal({
        content: '请输入手机号码'
      });
      return;
    }
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(mobile)) {
      wx.showModal({
        content: '手机号格式不正确'
      });
      return false;
    }

    wx.request({
      url: api.SmsCode,
      data: {
        phone: this.data.mobile
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log(res);
        console.log(res.header);
        wx.removeStorageSync('sessionid');
        wx.setStorageSync("sessionid", res.header["Set-Cookie"]);

        if (res.data.Status == 100) {
          wx.showToast({
            title: res.data.Data,
            icon: 'success',
            duration: 1000
          })
          var pages = getCurrentPages()
          var i = 60;
          var intervalId = setInterval(function () {
            i--
            if (i <= 0) {
              pages[pages.length - 1].setData({
                disableGetMobileCode: false,
                disableSubmitMobileCode: false,
                getCodeButtonText: '发送'
              })
              clearInterval(intervalId)
            } else {
              pages[pages.length - 1].setData({
                getCodeButtonText: i + "秒",
                disableGetMobileCode: true,
                disableSubmitMobileCode: false
              })
            }
          }, 1000);
        } else {
          wx.showToast({
            title: '发送失败',
            icon: 'none',
            duration: 1000
          })
        }

      }
    })
  },
  set_password:function(){
    var that=this;
    //SetUserPass
    var code=that.data.code;
    var mobile=that.data.mobile;
    var password=that.data.password;
    var userInfo=wx.getStorageSync('userInfo');
    var uid='';
    if(userInfo!="")
    {
      uid=userInfo.Id;
    }
    if (password == '' || password == undefined) {
      wx.showModal({content: '请输入支付密码'});
      return;
    }
 
    var pd={UserPass:password, UserID:uid,Code:code};
    wx.request({
      url: api.SetUserPass,
      data: pd,
      header: {
        'content-type': 'application/json',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) 
      {
        if (res.data.Status == 100) 
        {
          wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '支付密码设置成功',
            success: function (res) 
            {
                 //wx.navigateBack({delta: 1})
                 wx.reLaunch({
                   url: '/pages/start/index',
                 })
            }
        })
        }
        else 
        {
          wx.showModal({content: res.data.Msg})
        }
      }
    })

  },
  go_back:function() 
  {
    wx.navigateBack({delta: 1});
  }
})